<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- 
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License. 
-->
<html>
<head>
    <link type="text/css" rel="stylesheet" href="https://struts.apache.org/css/default.css">
    <style type="text/css">
        .dp-highlighter {
            width:95% !important;
        }
    </style>
    <style type="text/css">
        .footer {
            background-image:      url('https://cwiki.apache.org/confluence/images/border/border_bottom.gif');
            background-repeat:     repeat-x;
            background-position:   left top;
            padding-top:           4px;
            color:                 #666;
        }
    </style>
    <link href='https://struts.apache.org/highlighter/style/shCoreStruts.css' rel='stylesheet' type='text/css' />
    <link href='https://struts.apache.org/highlighter/style/shThemeStruts.css' rel='stylesheet' type='text/css' />
    <script src='https://struts.apache.org/highlighter/js/shCore.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushPlain.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushXml.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushJava.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushJScript.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushGroovy.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushBash.js' type='text/javascript'></script>
    <script type="text/javascript">
        SyntaxHighlighter.defaults['toolbar'] = false;
        SyntaxHighlighter.all();
    </script>
    <script type="text/javascript" language="javascript">
        var hide = null;
        var show = null;
        var children = null;

        function init() {
            /* Search form initialization */
            var form = document.forms['search'];
            if (form != null) {
                form.elements['domains'].value = location.hostname;
                form.elements['sitesearch'].value = location.hostname;
            }

            /* Children initialization */
            hide = document.getElementById('hide');
            show = document.getElementById('show');
            children = document.all != null ?
                    document.all['children'] :
                    document.getElementById('children');
            if (children != null) {
                children.style.display = 'none';
                show.style.display = 'inline';
                hide.style.display = 'none';
            }
        }

        function showChildren() {
            children.style.display = 'block';
            show.style.display = 'none';
            hide.style.display = 'inline';
        }

        function hideChildren() {
            children.style.display = 'none';
            show.style.display = 'inline';
            hide.style.display = 'none';
        }
    </script>
    <title>Localizing Output</title>
</head>
<body onload="init()">
<table border="0" cellpadding="2" cellspacing="0" width="100%">
    <tr class="topBar">
        <td align="left" valign="middle" class="topBarDiv" align="left" nowrap>
            &nbsp;<a href="home.html">Home</a>&nbsp;&gt;&nbsp;<a href="tutorials.html">Tutorials</a>&nbsp;&gt;&nbsp;<a href="bootstrap.html">Bootstrap</a>&nbsp;&gt;&nbsp;<a href="localizing-output.html">Localizing Output</a>
        </td>
        <td align="right" valign="middle" nowrap>
            <form name="search" action="https://www.google.com/search" method="get">
                <input type="hidden" name="ie" value="UTF-8" />
                <input type="hidden" name="oe" value="UTF-8" />
                <input type="hidden" name="domains" value="" />
                <input type="hidden" name="sitesearch" value="" />
                <input type="text" name="q" maxlength="255" value="" />
                <input type="submit" name="btnG" value="Google Search" />
            </form>
        </td>
    </tr>
</table>

<div id="PageContent">
    <div class="pageheader" style="padding: 6px 0px 0px 0px;">
        <!-- We'll enable this once we figure out how to access (and save) the logo resource -->
        <!--img src="/wiki/images/confluence_logo.gif" style="float: left; margin: 4px 4px 4px 10px;" border="0"-->
        <div style="margin: 0px 10px 0px 10px" class="smalltext">Apache Struts 2 Documentation</div>
        <div style="margin: 0px 10px 8px 10px"  class="pagetitle">Localizing Output</div>

        <div class="greynavbar" align="right" style="padding: 2px 10px; margin: 0px;">
            <a href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=23507">
                <img src="https://cwiki.apache.org/confluence/images/icons/notep_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Edit Page"></a>
            <a href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=23507">Edit Page</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/listpages.action?key=WW">
                <img src="https://cwiki.apache.org/confluence/images/icons/browse_space.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Browse Space"></a>
            <a href="https://cwiki.apache.org/confluence/pages/listpages.action?key=WW">Browse Space</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/createpage.action?spaceKey=WW&fromPageId=23507">
                <img src="https://cwiki.apache.org/confluence/images/icons/add_page_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Add Page"></a>
            <a href="https://cwiki.apache.org/confluence/pages/createpage.action?spaceKey=WW&fromPageId=23507">Add Page</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/createblogpost.action?spaceKey=WW&fromPageId=23507">
                <img src="https://cwiki.apache.org/confluence/images/icons/add_blogentry_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Add News"></a>
            <a href="https://cwiki.apache.org/confluence/pages/createblogpost.action?spaceKey=WW&fromPageId=23507">Add News</a>
        </div>
    </div>

    <div class="pagecontent">
        <div class="wiki-content">
            <div id="ConfluenceContent"><p>In the <a shape="rect" href="validating-input.html">Validating Input</a> lesson, we used the validation framework to verify data submitted from a form. In the <em>Localizing Output</em> lesson, we move the validation messages to a message resource bundle. </p>

<p>When creating web applications, we often find ourselves using the same messages or field labels on multiple pages. We may also want to localize the messages if the application is going to be used by people of different languages. </p>

<h2 id="LocalizingOutput-LocalizingValidationMessagesandFields">Localizing Validation Messages and Fields</h2>

<p>Let's add a message resource bundle and move into it the validation messages and field labels.</p>

<h3 id="LocalizingOutput-TheCode">The Code </h3>

<p>The framework associates message resources to classes. To add a message resource for the Logon action, we could just name the resource <code>Logon.properties</code> and set it on the classpath next to the Logon Action. </p>

<p>But, most people find it counter-productive to use separate message resource bundles for each class. Instead, many people prefer to add a bundle for an entire package of classes. To do this, simply add a <code>package.properties</code> file to the package. In our case, it would be the tutorial package.</p>

<div class="code panel pdl" style="border-style: solid;border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>tutorial/package.properties</b></div><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
requiredstring = ${getText(fieldName)} is required.
password = Password
username = User Name
</pre>
</div></div> 

<p>We also need to make changes to the validator and Logon page. As you see a value in resource bundle can also be specified as an expression.</p>

<h2 id="LocalizingOutput-Logon-validation.xml">Logon-validation.xml </h2>

<p><img class="emoticon emoticon-minus" src="https://cwiki.apache.org/confluence/s/en_GB/5982/f2b47fb3d636c8bc9fd0b11c0ec6d0ae18646be7.1/_/images/icons/emoticons/forbidden.png" data-emoticon-name="minus" alt="(minus)"> &lt;message&gt;Username is required&lt;/message&gt;<br clear="none">
<img class="emoticon emoticon-plus" src="https://cwiki.apache.org/confluence/s/en_GB/5982/f2b47fb3d636c8bc9fd0b11c0ec6d0ae18646be7.1/_/images/icons/emoticons/add.png" data-emoticon-name="plus" alt="(plus)"> &lt;message key="requiredstring"/&gt;</p>

<p><img class="emoticon emoticon-minus" src="https://cwiki.apache.org/confluence/s/en_GB/5982/f2b47fb3d636c8bc9fd0b11c0ec6d0ae18646be7.1/_/images/icons/emoticons/forbidden.png" data-emoticon-name="minus" alt="(minus)"> &lt;message&gt;Password is required&lt;/message&gt;<br clear="none">
<img class="emoticon emoticon-plus" src="https://cwiki.apache.org/confluence/s/en_GB/5982/f2b47fb3d636c8bc9fd0b11c0ec6d0ae18646be7.1/_/images/icons/emoticons/add.png" data-emoticon-name="plus" alt="(plus)"> &lt;message key="requiredstring"/&gt;</p>

<h2 id="LocalizingOutput-Logon.jsp">Logon.jsp </h2>

<p><img class="emoticon emoticon-minus" src="https://cwiki.apache.org/confluence/s/en_GB/5982/f2b47fb3d636c8bc9fd0b11c0ec6d0ae18646be7.1/_/images/icons/emoticons/forbidden.png" data-emoticon-name="minus" alt="(minus)"> &lt;s:textfield label="User Name" name="username"/&gt;<br clear="none">
<img class="emoticon emoticon-plus" src="https://cwiki.apache.org/confluence/s/en_GB/5982/f2b47fb3d636c8bc9fd0b11c0ec6d0ae18646be7.1/_/images/icons/emoticons/add.png" data-emoticon-name="plus" alt="(plus)"> &lt;s:textfield label="%{getText('username')}" name="username"/&gt;</p>

<p><img class="emoticon emoticon-minus" src="https://cwiki.apache.org/confluence/s/en_GB/5982/f2b47fb3d636c8bc9fd0b11c0ec6d0ae18646be7.1/_/images/icons/emoticons/forbidden.png" data-emoticon-name="minus" alt="(minus)"> &lt;s:password label="Password" name="password" /&gt;<br clear="none">
<img class="emoticon emoticon-plus" src="https://cwiki.apache.org/confluence/s/en_GB/5982/f2b47fb3d636c8bc9fd0b11c0ec6d0ae18646be7.1/_/images/icons/emoticons/add.png" data-emoticon-name="plus" alt="(plus)"> &lt;s:password label="%{getText('password')}" name="password" /&gt;</p>

<h3 id="LocalizingOutput-HowtheCodeWorks">How the Code Works</h3>

<ul><li>The "key" attribute tells the validator to check for a message resource bundle.</li><li>In the resource bundle, the expression 
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre>${getText(fieldName)}</pre>
</div></div>
 tells the framework to lookup the field name in the bundle too. This way we can use the same default message for all the <code>requiredstring</code> validators.</li><li>Likewise, in the text filed, the expression 
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre>%{getText('password')}</pre>
</div></div>
 tells the framework to lookup "password" in the message resources.</li></ul>


<h2 id="LocalizingOutput-LocalizingOtherMessages">Localizing Other Messages</h2>

<p>Other page elements can be localized too. For example, we could add the "Hello World" and the "Missing page" message to the bundle. </p>

<h3 id="LocalizingOutput-TheCode.1">The Code</h3>

<div class="code panel pdl" style="border-style: solid;border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>tutorial/package.properties</b></div><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
# ... 
HelloWorld.message = Struts is up and running ...
Missing.message = This feature is under construction. Please try again in the next iteration.
</pre>
</div></div> 

<p>This will work for <code>HelloWorld</code> since it is already in the tutorial package. But it won't work for the default Missing action, unless we add our own base class for the tutorial package. </p>

<div class="code panel pdl" style="border-style: solid;border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>TutorialSupport.java</b></div><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
package tutorial;
import com.opensymphony.xwork2.ActionSupport;
public class TutorialSupport extends ActionSupport {}
</pre>
</div></div> 

<p>And update the default wildcard mapping. </p>

<div class="code panel pdl" style="border-style: solid;border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>struts.xml</b></div><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
&lt;action name="*" class="tutorial.TutorialSupport"&gt;
  &lt;result&gt;/{1}.jsp&lt;/result&gt;
&lt;/action&gt;
</pre>
</div></div>

<p>Now, we can update <code>HelloWorld.jsp</code> and <code>Missing.jsp</code> to lookup the messages.</p>

<h4 id="LocalizingOutput-Missing.jsp">Missing.jsp </h4>

<p><img class="emoticon emoticon-minus" src="https://cwiki.apache.org/confluence/s/en_GB/5982/f2b47fb3d636c8bc9fd0b11c0ec6d0ae18646be7.1/_/images/icons/emoticons/forbidden.png" data-emoticon-name="minus" alt="(minus)"> This feature is under construction. Please try again in the next iteration. <br clear="none">
<img class="emoticon emoticon-plus" src="https://cwiki.apache.org/confluence/s/en_GB/5982/f2b47fb3d636c8bc9fd0b11c0ec6d0ae18646be7.1/_/images/icons/emoticons/add.png" data-emoticon-name="plus" alt="(plus)"> &lt;s:text name="Missing.message"/&gt;</p>

<p>In the case of HelloWorld, we set the message from the Action class. Let's update the class to use the message resource instead. </p>

<div class="code panel pdl" style="border-style: solid;border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>HelloWorld.java</b></div><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
package tutorial;

public class HelloWorld extends TutorialSupport {

    public static final String MESSAGE = "HelloWorld.message";

    public String execute() throws Exception {
        setMessage(getText(MESSAGE));
        return SUCCESS;
    }

  // ... 
}
</pre>
</div></div>

<h3 id="LocalizingOutput-HowtheCodeWorks.1">How the Code Works </h3>

<ul><li>For <code>Missing.jsp</code>, we used the <code>text</code> tag to lookup the message from the resource bundle.</li></ul>


<ul><li>For HelloWorld, we use the <code>getText</code> method in the Action class to lookup the message.</li><li>The HelloWorld.jsp displayed the message set by the Action, so it didn't need to change at all.</li></ul>


<h2 id="LocalizingOutput-WhattoRemember">What to Remember </h2>

<p>The framework is internationalized. To localize an application, we add the resource bundles, and update some elements or tags to refer to the bundles instead of static text. </p>

<p><img class="emoticon emoticon-light-on" src="https://cwiki.apache.org/confluence/s/en_GB/5982/f2b47fb3d636c8bc9fd0b11c0ec6d0ae18646be7.1/_/images/icons/emoticons/lightbulb_on.png" data-emoticon-name="light-on" alt="(lightbulb)"> For more, see <a shape="rect" href="localization.html">Localization</a> in the Core Developers Guide.</p>

<div class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1" class="confluenceTh"><p> Next </p></th><td colspan="1" rowspan="1" class="confluenceTd"><p> <a shape="rect" href="flying-solo.html">Flying Solo</a> </p></td></tr><tr><th colspan="1" rowspan="1" class="confluenceTh"><p> Prev </p></th><td colspan="1" rowspan="1" class="confluenceTd"><p> <a shape="rect" href="validating-input.html">Validating Input</a> </p></td></tr></tbody></table></div></div>
        </div>

        
    </div>
</div>
<div class="footer">
    Generated by CXF SiteExporter
</div>
</body>
</html>
